package com.iis.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

//import javafx.scene.layout.Border;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTable;

public class MainWindow extends JFrame {

	private JPanel contentPane;
	private JTextField uidTF;
	private JTextField passTF;
	private static MainWindow frame;
	private static JPanel mainPanel;
	private static JLabel lblTitle;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginForm loginForm = null;
					frame = new MainWindow(loginForm);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainWindow(LoginForm LoginFrame) {
		//getContentPane().setLayout(null);
		frame = this;
		setVisible(true);
		setTitle("Insurance Information System [IIS]");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 600, 500);
		setSize(600, 500);
		contentPane = new JPanel();
		contentPane.setLayout(new BorderLayout());
		
		// MENUBAR
		JMenuBar menuBar = new JMenuBar();
		JMenu mnSystem = new JMenu("System");
		JMenuItem mniDashboard = new JMenuItem("Dashboard");
		JMenuItem mniTransaction = new JMenuItem("Transactions List");
		JMenuItem mniLogout = new JMenuItem("Logout");
		mnSystem.add(mniDashboard);
		mnSystem.add(mniTransaction);
		mnSystem.add(mniLogout);
		
		JMenu mnStaffs = new JMenu("Staffs");
		JMenuItem mniStaffs = new JMenuItem("Manage Staffs");
		mnStaffs.add(mniStaffs);
		
		JMenu mnAgents = new JMenu("Agents");
		JMenuItem mniAgents = new JMenuItem("Manage Agents");
		mnAgents.add(mniAgents);
		
		JMenu mnClients = new JMenu("Clients");
		JMenuItem mniClients = new JMenuItem("Manage Clients");
		mnClients.add(mniClients);
		
		menuBar.add(mnSystem);
		menuBar.add(mnStaffs);
		menuBar.add(mnAgents);
		menuBar.add(mnClients);
		
		// MAIN CONTENT
		mainPanel = new JPanel();
		mainPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		mainPanel.setLayout(null);
		
		ShowDashboard(mainPanel);
		//ShowStaffsTable(mainPanel);
		
		contentPane.add(menuBar,BorderLayout.NORTH);
		contentPane.add(mainPanel,BorderLayout.CENTER);
		setContentPane(contentPane);

		frame.setPreferredSize(getSize());
		frame = this;
		
		// mniDashboard Action
		mniDashboard.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				mainPanel.removeAll();
				ShowDashboard(mainPanel);
				mainPanel.repaint();
				frame.pack();
			}
		});
		
		// mniTransaction Transactions List
		mniTransaction.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				mainPanel.removeAll();
				ShowTransactions.ShowTransactionsTable(mainPanel);
				mainPanel.repaint();
				frame.pack();
			}
		});
		
		// mniLogout Logout
		mniLogout.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(LoginFrame != null){
					frame.setVisible(false);
					LoginFrame.setVisible(true);
				}
			}
		});
		
		// mniStaffs Manage Staffs
		mniStaffs.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				mainPanel.removeAll();
				ManageStaff.ShowStaffsTable(mainPanel);
				mainPanel.repaint();
				frame.pack();
			}
		});
		
		// mniAgents Manage Agents
		mniAgents.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				mainPanel.removeAll();
				ManageAgent.ShowAgentsTable(mainPanel);
				mainPanel.repaint();
				frame.pack();
			}
		});
		
		// mniClients Manage Clients
		mniClients.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				mainPanel.removeAll();
				ManageClient.ShowClientsTable(mainPanel);
				mainPanel.repaint();
				frame.pack();
			}
		});
	}
	
	public void ShowDashboard(JPanel parent){
		JPanel borderPanel = new JPanel();
		borderPanel.setLayout(null);
		borderPanel.setBounds(10, 10, 565, 420);
		borderPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1, true));
		
		JPanel borderPanel2 = new JPanel();
		borderPanel2.setLayout(new BorderLayout());
		borderPanel2.setBounds(20, 20, 525, 60);
		borderPanel2.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1, true));
		
		lblTitle = new JLabel("IIS - Insurance Information System");
		lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblTitle.setFont(lblTitle.getFont().deriveFont(28.0f));
		borderPanel2.add(lblTitle, BorderLayout.CENTER);
		
		JPanel borderPanel3 = new JPanel();
		borderPanel3.setLayout(new GridLayout(2, 0, 0, -40));
		borderPanel3.setBounds(20, 100, 525, 120);
		borderPanel3.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1, true));
		
		JLabel lblTitle1 = new JLabel("Total Insurance Transaction : " + 10);
		lblTitle1.setHorizontalAlignment(SwingConstants.CENTER);
		lblTitle1.setFont(lblTitle1.getFont().deriveFont(18.0f));
		
		JLabel lblTitle2 = new JLabel("Total Sales : " + 10000);
		lblTitle2.setHorizontalAlignment(SwingConstants.CENTER);
		lblTitle2.setFont(lblTitle2.getFont().deriveFont(18.0f));
		
		borderPanel3.add(lblTitle1);
		borderPanel3.add(lblTitle2);
		
		borderPanel.add(borderPanel2);
		borderPanel.add(borderPanel3);
		mainPanel.add(borderPanel);
	}
}
